package com.tristia.entity;

import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

@Entity
@Table(name = "state")
@NamedQueries( { @NamedQuery(name = "State.findAll", query = "SELECT p FROM State p"),
	@NamedQuery(name = "State.findValidStateByUserID", query = "SELECT p FROM State p where p.progress!=:progress and p.user_id=:user_id"),
	@NamedQuery(name = "State.findValidStateByBookID", query = "SELECT p FROM State p where p.progress!=:progress and p.book_id=:book_id")
	})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "state", propOrder = {
	"id",
	"username",
    "rating",
    "favorite",
    "progress",
    "datetime",
    "book_id",
    "user_id"
})
@XmlRootElement
public class State implements ItemInfo {

	@Id 
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	@Transient
    private String username;
    private Float rating;
    private Boolean favorite = false;
    private String progress = "NONE";
    private Date datetime = Calendar.getInstance().getTime();
    private Long book_id;
    private Long user_id;
    @XmlTransient
    @ManyToOne
    @JoinColumn(name="book_id",insertable=false,updatable=false)
    private Book book;
    @XmlTransient
    @ManyToOne
    @JoinColumn(name="user_id",insertable=false,updatable=false)
    private User user;
	
    public State() {
    }

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public Float getRating() {
		return rating;
	}

	public void setRating(Float rating) {
		this.rating = rating;
	}

	public Boolean getFavorite() {
		return favorite;
	}

	public void setFavorite(Boolean favorite) {
		this.favorite = favorite;
	}

	public String getProgress() {
		return progress;
	}

	public void setProgress(String progress) {
		this.progress = progress;
	}

	public Date getDatetime() {
		return datetime;
	}

	public void setDatetime(Date datetime) {
		this.datetime = datetime;
	}

	public Long getBook_id() {
		return book_id;
	}

	public void setBook_id(Long book_id) {
		this.book_id = book_id;
	}

	public Long getUser_id() {
		return user_id;
	}

	public void setUser_id(Long user_id) {
		this.user_id = user_id;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	

	@Override
	public String toString() {
        return (new Formatter()).format(
                "{\"id\":\"%d\",\"title\":\"%s}",
                this.getId(), this.getRating()).toString();
	}
	
	@GET
    @Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
    public State produceXml() {
        return this;
    }

}
